<@pp.dropOutputFile/>
<#include "../common/validation.ftl">

<#-- generate package name -->

<#-- iterate table list-->
<#list doc.tables as table>
<#assign module = doc.artifactid?replace(".", "/") + "/" + table.moduleid?uncap_first>
<#assign fileName = table.name?cap_first>
<#assign data_url = module + "/" + fileName>
<@pp.changeOutputFile name="/main/webapp/" + data_url + "/input_js.js"/>
<#noparse>
var ADD_CHECKER = null;
$(function(){	
	// Validators
	var options = {
		rules : </#noparse>${getRules(table)},
		message : ${getMessages(table)}<#noparse>,
		errorElement: "em",
		errorPlacement: function(error, element ) {
			error.addClass( "help-block" );
			error.insertAfter(element.parent("div"));
		},
		highlight: function ( element, errorClass, validClass ) {
			$(element).parents(".form-group").addClass("has-error").removeClass("has-success");
		},
		unhighlight: function (element, errorClass, validClass) {
			$(element).parents(".form-group").addClass("has-success").removeClass("has-error");
		}
	};
	ADD_CHECKER = $("#addForm").validate(options);
	$(".form_datetime").datetimepicker({format: 'yyyy-mm-dd hh:ii'});
});

function showTip(success, title, message){
	hidTip();
	var $tip_window;
	if(success){
		$tip_window = $("#alert_success");
		$tip_window.removeClass("hidden").addClass("show");
	}else{
		$tip_window = $("#alert_fail");
		$tip_window.removeClass("hidden").addClass("show");
	}
	$tip_window.text(message);
	setTimeout("hidTip()",1000*5);
}
function hidTip(){
	$("#alert_success, #alert_fail").removeClass("show").addClass("hidden");
}
function array2map(array){
	var m = {};
	for(a in array){
		m[array[a].name] = array[a].value;
	}
	return m;
}
function add(){
	if(ADD_CHECKER.form()){
		$.post("</#noparse>/${app}/${data_url}<#noparse>/create.action", 
			array2map($("#addForm").serializeArray()), 
			function(data){
				if(data.success == 0){
				}
				showTip(data.success == 0, "操作结果", data.message);				
			},
			"json"
		);
	}
}
//Load  
function loading(tip) {  
}  
  
//hidden Load  
function loaded() {  
}  
</#noparse>
</#list>